package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.common.pojo.ResponseResult;
import com.lg.product.entity.Brand;
import com.lg.product.entity.CategoryBrand;
import com.lg.product.entity.CategoryVo;
import com.lg.product.service.IBrandService;
import com.lg.product.service.ICategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品牌表，一个品牌下有多个商品（spu），一对多关系 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    IBrandService iBrandService;
    @Autowired
    ICategoryBrandService iCategoryBrandService;

    @RequestMapping("/list")
    public ResponseResult list(Page page, Brand brand){

        QueryWrapper<Brand> wrapper = new QueryWrapper<>();

        if(brand.getName()!=null && !brand.equals(""))
        wrapper.like("name",brand.getName());


        Page page1 = iBrandService.page(page, wrapper);

        return ResponseResult.success(page1);
    }


    @RequestMapping("/insert")
    @Transactional
    public ResponseResult insert(@RequestBody Brand brand){
        System.out.println("brand"+brand);
        try {
            //先保存主表
            iBrandService.save(brand);
            System.out.println(brand.getIds());
            if(brand.getIds()!=null && !brand.getIds().equals("")){
                //拆分数组
                String[] ids = brand.getIds().split(",");
                ArrayList<CategoryBrand> cbList = new ArrayList<>();
                for(String id:ids){
                    CategoryBrand cb = new CategoryBrand();
                    cb.setBrandId(brand.getId());
                    cb.setCategoryId(Long.parseLong(id));
                    cbList.add(cb);
                }
                iCategoryBrandService.saveBatch(cbList);
            }
            return ResponseResult.success(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.error("false");
    }




    @RequestMapping("/selectCategoryIdsByBrandId")
    public ResponseResult selectCategoryIdsByBrandId(Long brandId){

        List<CategoryVo> categoryVos = iCategoryBrandService.selectCategoryIdsByBrandId(brandId);

        return ResponseResult.success(categoryVos);
    }



    @RequestMapping("/update")
    @Transactional
    public ResponseResult update(@RequestBody Brand brand){

        try {
        //获取品牌id
        Long id1 = brand.getId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("brand_id",id1);
        //根据品牌id删除中间表
        iCategoryBrandService.remove(wrapper);
        //跟新主表
            iBrandService.updateById(brand);
        if(brand.getIds()!=null && !brand.getIds().equals("")){
        }
                //拆分数组
                String[] ids = brand.getIds().split(",");
                ArrayList<CategoryBrand> cbList = new ArrayList<>();
                for(String id:ids){
                    CategoryBrand cb = new CategoryBrand();
                    cb.setBrandId(brand.getId());
                    cb.setCategoryId(Long.parseLong(id));
                    cbList.add(cb);
                }
                iCategoryBrandService.saveBatch(cbList);

            return ResponseResult.success(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.error("false");
    }



    @RequestMapping("/delete")
    public ResponseResult delete(Long id){
        try {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("brand_id",id);
            iCategoryBrandService.remove(wrapper);
            iBrandService.removeById(id);
            return ResponseResult.success(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseResult.error("false");
    }

    @RequestMapping("/findAll")
    public ResponseResult findAll(){
        return ResponseResult.success(iBrandService.list());
    }

}
